package com.googlecode.sqldatagenerator.parser;

import com.googlecode.sqldatagenerator.util.StringUtil;

public class SyntaxGroup implements Cloneable {

    protected String startToken;
    
    protected String endToken;
    
    protected int openCount;
    
    protected int closeCount;
    
    protected StringBuilder currentText;
    
    public SyntaxGroup(String startToken, String endToken, StringBuilder textBuffer) {
        this.startToken = startToken;
        this.endToken = endToken;
        this.currentText = textBuffer;
    }
    
    public void reset(){
        openCount = 0;
        closeCount = 0;
        if (currentText != null){
            currentText.setLength(0);
        }
    }
    
    public boolean isStartToken(CharSequence token){
        return StringUtil.equals(token, startToken);
    }

    public boolean isEndToken(CharSequence token){
        return StringUtil.equals(token, endToken);
    }

    public String getStartToken() {
        return startToken;
    }

    public String getEndToken() {
        return endToken;
    }

    public SyntaxGroup getParent() {
        return null;
    }

    public StringBuilder getCurrentText() {
        return currentText;
    }

    public void setCurrentText(StringBuilder currentText) {
        this.currentText = currentText;
    }

    public int getOpenCount() {
        return openCount;
    }

    public int getCloseCount() {
        return closeCount;
    }
    
    public boolean isOpened() {
        return openCount != closeCount;
    }
    
    @Override
    public SyntaxGroup clone() {
        try {
            SyntaxGroup r = (SyntaxGroup) super.clone();
            r.closeCount = 0;
            r.openCount = 0;
            if (currentText != null){
                r.currentText = new StringBuilder();
            }
            return r;
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException(e);
        }
    }

}
